var server = require('http').createServer()
  , url = require('url')
  , WebSocketServer = require('ws').Server
  , wss = new WebSocketServer({ port: 6801 })
 // , express = require('express')
//  , webServer = express()
  //, port = 6801;
   var aria2 = require("./aria2.js");

//webServer.use(express.static(__dirname + '/public'));
wss.on('connection', function connection(ws) {
  var location = url.parse(ws.upgradeReq.url, true);
  //console.log(location);
  ws.on('message', function incoming(message) {
    //console.log('received: %s', message);
    var result = JSON.parse(message);

    if(toString.apply(result) === '[object Array]'){
        var retval = [];
        result.forEach(function(obj){
            eval('var ret= '+ obj.method+'(obj)');
            retval.push(ret);
        });
    }else{
        var method = result.method;
        eval('var retval= '+ result.method+'(result)');
    }
    ws.send(JSON.stringify(retval));
   // result.method(result.id, result.params);
  });
  //ws.send('something');
});
//server.on('request', webServer);
//server.listen(port, function () { console.log('Listening on ' + server.address().port) });
